/*
 * This source code is a product of Sun Microsystems, Inc. and is provided
 * for unrestricted use.  Users may copy or modify this source code without
 * charge.
 *
 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 *
 * Sun source code is provided with no support and without any obligation on
 * the part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 *
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
 * OR ANY PART THEREOF.
 *
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even if
 * Sun has been advised of the possibility of such damages.
 *
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */

/*
 * g726_32.c
 *
 * Description:
 *
 * g721_encoder(), g721_decoder()
 *
 * These routines comprise an implementation of the CCITT G.721 ADPCM
 * coding algorithm.  Essentially, this implementation is identical to
 * the bit level description except for a few deviations which
 * take advantage of work station attributes, such as hardware 2's
 * complement arithmetic and large memory.  Specifically, certain time
 * consuming operations such as multiplications are replaced
 * with lookup tables and software 2's complement operations are
 * replaced with hardware 2's complement.
 *
 * The deviation from the bit level specification (lookup tables)
 * preserves the bit level performance specifications.
 *
 * As outlined in the G.721 Recommendation, the algorithm is broken
 * down into modules.  Each section of code below is preceded by
 * the name of the module which it is implementing.
 *
 * The ITU-T G.726 coder is an adaptive differential pulse code modulation
 * (ADPCM) waveform coding algorithm, suitable for coding of digitized
 * telephone bandwidth (0.3-3.4 kHz) speech or audio signals sampled at 8 kHz.
 * This coder operates on a sample-by-sample basis. Input samples may be
 * represented in linear PCM or companded 8-bit G.711 (m-law/A-law) formats
 * (i.e., 64 kbps). For 32 kbps operation, each sample is converted into a
 * 4-bit quantized difference signal resulting in a compression ratio of
 * 2:1 over the G.711 format. For 24 kbps 40 kbps operation, the quantized
 * difference signal is 3 bits and 5 bits, respectively.
 *
 * $Log: g726_32.c,v $
 * Revision 1.5  2002/11/20 04:29:13  robertj
 * Included optimisations for G.711 and G.726 codecs, thanks Ted Szoczei
 *
 * Revision 1.1  2002/02/11 23:24:23  robertj
 * Updated to openH323 v1.8.0
 *
 * Revision 1.2  2002/02/10 21:14:54  dereks
 * Add cvs log history to head of the file.
 * Ensure file is terminated by a newline.
 *
 *
 *
 */
package io.github.hylexus.jt.jt1078.support.extension.audio.impl.converters.g7xx;

import static io.github.hylexus.jt.jt1078.support.extension.audio.impl.converters.g7xx.G711.alaw2linear;
import static io.github.hylexus.jt.jt1078.support.extension.audio.impl.converters.g7xx.G711.ulaw2linear;

/**
 * 这个包中的代码是从以下几个代码库/资料库中复制、修改的:
 * <ul>
 *     <li><a href="https://gitee.com/matrixy/jtt1078-video-server">https://gitee.com/matrixy/jtt1078-video-server</a></li>
 *     <li><a href="https://github.com/fredrikhederstierna/g726">https://github.com/fredrikhederstierna/g726</a></li>
 * </ul>
 *
 * @see <a href="https://github.com/fredrikhederstierna/g726/blob/41d813c33e2001515e7992bc39423250996e258b/src/g726_32.c#L163">https://github.com/fredrikhederstierna/g726/blob/41d813c33e2001515e7992bc39423250996e258b/src/g726_32.c#L163</a>
 * @see <a href="https://gitee.com/matrixy/jtt1078-video-server/blob/flv/src/main/java/cn/org/hentai/jtt1078/codec/g726/G726_32.java#L32">https://gitee.com/matrixy/jtt1078-video-server/blob/flv/src/main/java/cn/org/hentai/jtt1078/codec/g726/G726_32.java#L32</a>
 */
public class G726_32 extends G72X {
    private static final G726_32 instance = new G726_32();

    public static G726_32 getInstance() {
        return instance;
    }

    static int[] qtab_721 = {-124, 80, 178, 246, 300, 349, 400};

    /*
     * Maps G.721 code word to reconstructed scale factor normalized log
     * magnitude values.
     */
    static short[] _dqlntab = {-2048, 4, 135, 213, 273, 323, 373, 425, 425, 373, 323, 273, 213, 135, 4, -2048};
    /* Maps G.721 code word to log of scale factor multiplier. */
    static short[] _witab = {-12, 18, 41, 64, 112, 198, 355, 1122, 1122, 355, 198, 112, 64, 41, 18, -12};
    /*
     * Maps G.721 code words to a set of values whose long and short
     * term averages are computed and then compared to give an indication
     * how stationary (steady state) the signal is.
     */
    static short[] _fitab = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00, 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};

    /*
     * g721_encoder()
     *
     * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
     * the resulting code. -1 is returned for unknown input coding value.
     */
    int g726_32_encoder(int sl, int in_coding, G726State state_ptr) {
        int sezi;
        int sez;            /* ACCUM */
        int se;
        int d;                /* SUBTA */
        int y;                /* MIX */
        int i;
        int dq;
        int sr;                /* ADDB */
        int dqsez;            /* ADDC */

        switch (in_coding) {    /* linearize input sample to 14-bit PCM */
            case AUDIO_ENCODING_ALAW:
                sl = alaw2linear(sl) >> 2;
                break;
            case AUDIO_ENCODING_ULAW:
                sl = ulaw2linear(sl) >> 2;
                break;
            case AUDIO_ENCODING_LINEAR:
                sl >>= 2;            /* 14-bit dynamic range */
                break;
            default:
                return (-1);
        }

        sezi = predictor_zero(state_ptr);
        sez = sezi >> 1;
        se = (sezi + predictor_pole(state_ptr)) >> 1;    /* estimated signal */

        d = sl - se;                /* estimation difference */

        /* quantize the prediction difference */
        y = step_size(state_ptr);        /* quantizer step size */
        i = quantize(d, y, qtab_721, 7);    /* i = ADPCM code */

        dq = reconstruct(i & 8, _dqlntab[i], y);    /* quantized est diff */

        sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;    /* reconst. signal */

        dqsez = sr + sez - se;            /* pole prediction diff. */

        update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);

        return (i);
    }

    @Override
    public int decode(byte[] in_buff, int in_offset, int in_len, int out_coding, byte[] out_buff, int out_offset) {
        return this.decode(in_buff, in_offset, in_len, out_coding, out_buff, out_offset, new G726State());
    }

    int decode(byte[] in_buff, int in_offset, int in_len, int out_coding, byte[] out_buff, int out_offset, G726State state) {
        if (out_coding == AUDIO_ENCODING_ALAW || out_coding == AUDIO_ENCODING_ULAW) {
            for (int i = 0; i < in_len; i++) {
                int in_value = unsignedInt(in_buff[in_offset + i]);
                int out_value1 = g726_32_decoder(in_value >> 4, out_coding, state);
                int out_value2 = g726_32_decoder(in_value & 0xF, out_coding, state);
                int out_index = out_offset + i * 2;
                out_buff[out_index] = (byte) out_value1;
                out_buff[out_index + 1] = (byte) out_value2;
            }
            return in_len * 2;
        } else if (out_coding == AUDIO_ENCODING_LINEAR) {
            for (int i = 0; i < in_len; i++) {
                int in_value = unsignedInt(in_buff[in_offset + i]);
                // int out_value1=G711.ulaw2linear(decode(in_value>>4,AUDIO_ENCODING_ULAW,state));
                // int out_value2=G711.ulaw2linear(decode(in_value&0xF,AUDIO_ENCODING_ULAW,state));
                int out_value1 = g726_32_decoder(in_value >> 4, out_coding, state);
                int out_value2 = g726_32_decoder(in_value & 0xF, out_coding, state);
                int out_index = out_offset + i * 4;
                out_buff[out_index] = (byte) (out_value1 & 0xFF);
                out_buff[out_index + 1] = (byte) (out_value1 >> 8);
                out_buff[out_index + 2] = (byte) (out_value2 & 0xFF);
                out_buff[out_index + 3] = (byte) (out_value2 >> 8);
            }
            return in_len * 4;
        } else return -1;
    }
    /*
     * g721_decoder()
     *
     * Description:
     *
     * Decodes a 4-bit code of G.721 encoded data of i and
     * returns the resulting linear PCM, A-law or u-law value.
     * return -1 for unknown out_coding value.
     */

    int g726_32_decoder(int i, int outCoding, G726State state) {
        int sezi;
        int sez;            /* ACCUM */
        int sei;
        int se;
        int y;                /* MIX */
        int dq;
        int sr;                /* ADDB */
        int dqsez;
        long lino;

        i &= 0x0f;                /* mask to get proper bits */
        sezi = predictor_zero(state);
        sez = sezi >> 1;
        sei = sezi + predictor_pole(state);
        se = sei >> 1;            /* se = estimated signal */

        y = step_size(state);    /* dynamic quantizer step size */

        dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */

        sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq;    /* reconst. signal */

        dqsez = sr - se + sez;            /* pole prediction diff. */

        update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state);

        switch (outCoding) {
            case AUDIO_ENCODING_ALAW:
                return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721));
            case AUDIO_ENCODING_ULAW:
                return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721));
            case AUDIO_ENCODING_LINEAR:
                // lino = (long) sr << 2;  /* this seems to overflow a short*/
                // lino = lino > 32767 ? 32767 : lino;
                // lino = lino < -32768 ? -32768 : lino;
                // return lino;//(sr << 2);	/* sr was 14-bit dynamic range */
                return (short) (sr << 2);
            default:
                return (-1);
        }
    }

    protected static int unsignedInt(byte b) {
        return ((int) b + 0x100) & 0xFF;
    }
}
